package com.cndw.kungfu.model.goods;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.domain.Goods;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.model.Context;

public class MarketCenter {
	
	private static Map<Long, PlayerMarket> marketCenter = new ConcurrentHashMap<Long, PlayerMarket>();
	
	public static void add(long playerId, PlayerMarket market) {
		marketCenter.put(playerId, market);
	}
	
	public static void remove(long playerId) {
		marketCenter.remove(playerId);
	}
	
	public Collection<PlayerMarket> getAll() {
		return marketCenter.values();
	}
	
	public Map<Long, PlayerMarket> getMarketCenter() {
		return marketCenter;
	}
	
	public static List<Object[]> getByType(int type) {
		List<Object[]> resultList = new ArrayList<Object[]>();
		for (PlayerMarket e : marketCenter.values()) {
			Player marketPlayer = Context.getPlayerServer().getByPlayerId(e.getPlayerId());
			for (Integer[] record : e.getPriceMap().values()) {//record[背包位置，摊位位置，价钱，交易类型(1:银两，2：金币),数量]
				Goods goods = marketPlayer.getPlayerBoxs().getByLock(record[0]);
				if (!BlankUtils.isBlank(goods)) {
					if (goods.getGoodsMode() == null) {
						continue;
					}
					if (type != 0 && goods.getGoodsMode().getGoodTypeId() != type) {
						continue;
					}
//					if (vId != 0 && goods.getGoodsMode().getVocationId() != vId) {
//						continue;
//					}
					Object[] dataObj = new Object[6];
					dataObj[0] = goods.getGoodsId();//物品Id
					dataObj[1] = marketPlayer.getPlayerName();//摆摊玩家名字
					dataObj[2] = e.getPlayerId();//摆摊玩家Id
					dataObj[3] = record[2];//价钱
					dataObj[4] = record[1];//摊位位置
					dataObj[5] = goods.toGoodsVO();//物品VO
//					dataObj[6] = record[4];//物品数量
					resultList.add(dataObj);
				}
			}
		}
		Collections.sort(resultList, new Comparator<Object[]>() {
			@Override
			public int compare(Object[] o1, Object[] o2) {
				return (Integer)o1[0] - (Integer)o2[0];
			}
		});
		
		return resultList;
	}

}
